<?php
/**
 * 测试类 MTest
 * 参考标准
 * @link https://www.ietf.org/rfc/rfc2898.txt
 *
 * php官方函数将在php5.5发布
 * @see http://php.net/manual/en/function.hash-pbkdf2.php
 * example: pbkdf2("sha256", 'password', 'salt', 1, 20);
 * result:120fb6cffcf8b32c43e7 (与php5.5内置的pbkdf2函数输出一至)
 *
 * @param string $algo             The hash algorithm to use. Recommended: SHA256
 * @param string $password         The password to use for the derivation.
 * @param string $salt             The salt to use for the derivation.
 * @param int     $iterations    The number of internal iterations to perform for the derivation.
 * @param int     $length        The length of the derived key to output. If 0, the length of the supplied algorithm is used.
 * @param boolean $raw_output    When set to TRUE, outputs raw binary data. FALSE outputs lowercase hexits.
 * @return string
 */

class Mtest extends osa_controller{


    public function index(){
        if(!isset($_SESSION['username'])){
            header("Location: index.php?c=login&a=index", TRUE, 302);
        }
    }

    public function mencryptfc(){

        $data['code'] = self::pbkdf2("sha256",'1qaz!QAZ','salt',1,20) ;
        $this->loadview('mtest/encrypt',$data);
    }

    public static function pbkdf2($algo, $password, $salt, $iterations, $length= 0, $raw_output = false)
    {
        $algo = strtolower($algo);
        if(!in_array($algo, hash_algos(), true))
            throw new Exception('PBKDF2 ERROR: Invalid hash algorithm.');
        if($iterations <= 0)
            throw new Exception('PBKDF2 ERROR: Invalid parameters.');
        // hash方式默认长度，对一个空值进行hash计算，得出
        // length in octets of pseudorandom function output, a positive integer
        $hlen = strlen(hash($algo, null, true));
        if ($length <=0) $length = $hlen;
        if ($length > (pow(2,32)-1)*$hlen)
            throw new Exception('PBKDF2 ERROR: derived key too long.');

        // 如果生成的加密值长度低于取值长度，就将加密再执行N遍以补足数据长度
        // N=取值长度除以加密方式对应的值长度加一法取整
        $block_count = ceil($length / $hlen);
        $output = "";
        for($i = 1; $i <= $block_count; $i++) {
            // $i encoded as 4 bytes, big endian.
            $last = $salt . pack("N", $i);
            // first iteration
            $last = $xorsum = hash_hmac($algo, $last, $password, true);
            // perform the other $count - 1 iterations
            for ($j = 1; $j < $iterations; $j++) {
                $xorsum ^= ($last = hash_hmac($algo, $last, $password, true));
            }
            $output .= $xorsum;
        }
        if($raw_output)
            return substr($output, 0, $length);
        else
            return substr(bin2hex($output), 0, $length);
    }

    public function tableData(){

        if(!isset($_SESSION['username'])){
            header("Location: index.php?c=login&a=index", TRUE, 302);
        }

        $this->model = $this->loadmodel('mtable') ;
        $this->page = $this->loadmodel('mpage') ;
        $list = $this->model->getTableIpList() ;

        $data['url'] = "index.php?c=mtest&a=tableItems" ;
        $data['list'] = $list ;
        $this->loadview('mtest/mtable',$data);

    }

    /**
     *  函数 sTableA1
     *  Ajax获取 Zabbix_items表中 items 数据 ;
     *  返回 JSON 数据
     */

    public function sTableA1(){
        $this->model = $this->loadmodel('mtable') ;
        $hostid = $_GET['hostid'] ;
        $items = $this->model->getStableA1($hostid) ;
        echo json_encode($items);
    }


    /**
     *  函数 sTableA2
     *  Ajax获取 Redis 指定的items 数据 ;
     *  返回 JSON 数据
     */

    public function sTableA2(){
        $itemsid = $_GET['itemsid'] ;
        $hostid = $_GET['hostid'] ;
        $timetype = $_GET['timetype'] ;

        $redis = new Redis();  // 创建 Redis
        $status = $redis->connect('10.10.83.162', 16379); //连接 Redis
        $listName = $hostid."_".$itemsid."_".$timetype ;  //组合成key值

        if($status==1){
            $redis->select(1) ; //选择第二个key
            $count = $redis->dbSize(); //判断key大小
            if($redis->exists($listName)){
                /*json_encode($redis->lRange($listName, 0, -1));*/  //返回JSON
                $jdata = $redis->lRange($listName, 0, -1) ;
                var_export($jdata);

            }
        }
    }


    public function tableItems(){

        if(!isset($_SESSION['username'])){
            header("Location: index.php?c=login&a=index", TRUE, 302);
        }

        $this->model = $this->loadmodel('mtable') ;
        $this->page = $this->loadmodel('mpage') ;
        $list = $this->model->getTableIpList() ;
        $itemList = $this->model->getTableItimeList($_POST["host_id"]) ;
        $data['url'] = "index.php?c=mtest&a=tableItems" ;
        $data['list'] = $list ;
        $data['itemList'] = $itemList ;
        $this->loadview('mtest/mtable',$data);

    }

    public function test(){

        if(!isset($_SESSION['username'])){
            header("Location: index.php?c=login&a=index", TRUE, 302);
        }

        $this->service = $this->loadService("db_service") ;
        $table_name = "auth_user" ;
        $para = array("first_name"=>"'first_name'","last_name"=>"'last_name'","date_joined"=>"'2016.09.05'","username"=>"'username0'","is_active"=>1,"is_staff"=>"1","is_superuser"=>0,"password"=>"'password'","email"=>"'13701902730@139.com'") ;

        //$para = array("id"=>24);

        //$as = $this->service->insertData($table_name,$para) ; // 插入数据

        $ds = $this->service->deleteData($table_name,$para);  // 删除数据

        $rs = $this->service->getall($table_name,$para) ;  // 获取数据

        var_export($rs);
    }




}